import uuid

from sqlalchemy.orm import Session

from api.model.user_llm_model import UserLLM
from api.utils.session import with_session
from api.vo.value.user_llm_value import UserLLMValue


@with_session
def save(session: Session, user_llm: UserLLM):
    llm = session.query(UserLLM).filter(UserLLM.user_id == user_llm.user_id).first()
    if llm:
        llm.chat_model = user_llm.chat_model
        llm.embedding_model = user_llm.embedding_model
        llm.img2txt_model = user_llm.img2txt_model
        llm.rerank_model = user_llm.rerank_model
        llm.sequence2txt_model = user_llm.sequence2txt_model
        llm.tts_model = user_llm.tts_model
        llm.user_id = user_llm.user_id
        return session.merge(llm)
    user_llm.id = str(uuid.uuid4())
    return session.add(user_llm)

@with_session
def get_user_id(session: Session, user_id: str):
    user_llm = session.query(UserLLM).filter(UserLLM.user_id == user_id).first()
    if user_llm:
        return UserLLMValue(id=user_llm.id, chatModel=user_llm.chat_model, embeddingModel=user_llm.embedding_model, img2txtModel=user_llm.img2txt_model, sequence2txtModel=user_llm.sequence2txt_model, rerankModel=user_llm.rerank_model, ttsModel=user_llm.tts_model)
    return None

